package com.xunbao.utils;


import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * ClassName:MailUtils
 * Description:
 * Author:yijiawei
 * Date: 2016/5/21 17:09
 */
public class MailUtils {


    public static void main(String[] args) {

        Transport trans = null;
        try {
            Properties properties = new Properties();
            properties.setProperty("mail.smtp.host", "smtp.qq.com");
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.transport.protocol", "smtp"); //设置访问服务器的协议

            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);

            Session session = Session.getDefaultInstance(properties);
            session.setDebug(true);

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("1398656012@qq.com"));
            message.setText("Hello world!"); //设置邮件内容
            message.setSubject("java mail"); //设置邮件主题

            trans = session.getTransport("smtps");
            trans.connect("smtp.qq.com", 465, "1398656012", "yijiawei2012"); //连接邮箱smtp服务器，25为默认端口
            trans.sendMessage(message, new Address[]{new InternetAddress("535332162@qq.com")}); //发送邮件


        } catch (MessagingException e) {
            e.printStackTrace();
            return;
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } finally {
            if (trans != null) {
                try {
                    trans.close();
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("send end .....");

    }

}
